package com.auto.learning.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.event.Handle;
import com.auto.learning.fragment.RemindFragment;
import com.auto.learning.fragment.SystemMessageFragment;
import com.auto.learning.service.MyUserInfo;
import com.auto.learning.tools.TemporaryVariable;

public class MyMessageActivity extends BaseActivity implements OnClickListener {
	
	private int type = 0;  // 0代表先显示消息，在点击显示通知， 1反之

	private Fragment currentFragment;
	public FragmentManager fm;
	private RemindFragment remindFragment;
	private SystemMessageFragment systemMessageFragment;
	
	private RelativeLayout rel_left,rel_right;
	private TextView tv_comment_remind_title,tv_comment_remind_num,tv_system_notify_title,tv_system_notify_num;
	private View system_notify_view, comment_remind_view;
	
	
	private Handle handler = new Handle(this){
		public void handleMessage(Message msg) {
			switch (msg.what) {
				
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_message_activity);
		initView();
		initIntentData();
		initFragment();
		initData();
		TemporaryVariable.activities.push(this);
	}
	
	private void initView() {
		type = getIntent().getIntExtra("msg_type", 0);
		setTitle(getResources().getString(R.string.menu_message));
		rel_left = (RelativeLayout) findViewById(R.id.rel_left);
		rel_right = (RelativeLayout) findViewById(R.id.rel_right);
		tv_comment_remind_title = (TextView) findViewById(R.id.tv_comment_remind_title);
		tv_comment_remind_num = (TextView) findViewById(R.id.tv_comment_remind_num);
		tv_system_notify_title = (TextView) findViewById(R.id.tv_system_notify_title);
		tv_system_notify_num = (TextView) findViewById(R.id.tv_system_notify_num);
		system_notify_view =  findViewById(R.id.system_notify_view);
		comment_remind_view =  findViewById(R.id.comment_remind_view);
		int remindNum = MyUserInfo.getInstance().getUserInfoBean().getRemindUnread();
		if (remindNum > 0) {
			tv_comment_remind_num.setText(remindNum+"");
			tv_comment_remind_num.setVisibility(View.VISIBLE);
		}else {
			tv_comment_remind_num.setVisibility(View.GONE);
		}
		int notifyNum = MyUserInfo.getInstance().getUserInfoBean().getNotifyUnread();
		if (notifyNum > 0) {
			tv_system_notify_num.setText(notifyNum+"");
			tv_system_notify_num.setVisibility(View.VISIBLE);
		}else {
			tv_system_notify_num.setVisibility(View.GONE);
		}
		rel_left.setOnClickListener(this);
		rel_right.setOnClickListener(this);
		if (type != 0) {
			tv_system_notify_title.setTextColor(getResources().getColor(R.color.main_green_color));
			system_notify_view.setBackgroundColor(getResources().getColor(R.color.main_green_color));
			tv_comment_remind_title.setTextColor(getResources().getColor(R.color.black));
			comment_remind_view.setBackgroundColor(getResources().getColor(R.color.white));
			switchContent(currentFragment, systemMessageFragment);
		}
	}
	
	/*public void HiddenRemindNotify(){
		if (tv_comment_remind_num != null) {
			tv_comment_remind_num.setVisibility(View.GONE);
		}
	}
	
	public void HiddenSystemNotify(){
		if (tv_system_notify_num != null) {
			tv_system_notify_num.setVisibility(View.GONE);
		}
	}*/
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rel_left:
			tv_comment_remind_title.setTextColor(getResources().getColor(R.color.main_green_color));
			comment_remind_view.setBackgroundColor(getResources().getColor(R.color.main_green_color));
			tv_system_notify_title.setTextColor(getResources().getColor(R.color.black));
			system_notify_view.setBackgroundColor(getResources().getColor(R.color.white));
			switchContent(currentFragment, remindFragment);
			break;
		case R.id.rel_right:
			tv_system_notify_title.setTextColor(getResources().getColor(R.color.main_green_color));
			system_notify_view.setBackgroundColor(getResources().getColor(R.color.main_green_color));
			tv_comment_remind_title.setTextColor(getResources().getColor(R.color.black));
			comment_remind_view.setBackgroundColor(getResources().getColor(R.color.white));
			switchContent(currentFragment, systemMessageFragment);
			break;
		}
	}

	
	
	@Override
	protected void onDestroy() {
		TemporaryVariable.activities.removeElement(this);
		super.onDestroy();
	}
	
	private void initIntentData(){
		Intent intent = getIntent();
	}

	private void initFragment() {
		fm = getSupportFragmentManager();
		remindFragment = new RemindFragment();
		systemMessageFragment = new SystemMessageFragment();
	}

	private void initData() {
		if (type != 0) {
			currentFragment = systemMessageFragment;
		}else {
			currentFragment = remindFragment;
		}
		initFragment(currentFragment);
	}

	private void initFragment(Fragment fragment) {
		 FragmentTransaction transaction = fm.beginTransaction();
		 transaction.replace(R.id.main_view, fragment);
		 transaction.commit();
	}

	public void switchContent(Fragment from, Fragment to) {
		if (currentFragment != to) {
			currentFragment = to;
			FragmentTransaction transaction = fm.beginTransaction();
			if (!to.isAdded()) { // 先判断是否被add过
				transaction.hide(from).add(R.id.main_view, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
			} else {
				transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
			}
		}
	}

}
